package actions;

import gui.Frame;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JTextField;

/**
 * An action to log the user into the system and display options
 * to them based on their usertype. 
 * 
 * @author Natalie
 *
 */
@SuppressWarnings("serial")
public class LoginAction extends AbstractAction {

	  private final Frame myFrame;
	  
	  /**
	 * The box the User will type their email into. 
	 */
	private final JTextField myEmailBox;
	  
	   /**
	   * Constructs a login clear action.
	   * 
	   * @param theEmailBox The box the user will type their email into. 
	   */
	  public LoginAction(final Frame theFrame, final JTextField theEmailBox) {  
	    super("Login");
	    myFrame = theFrame;
	    myEmailBox = theEmailBox;
	  }
	
	/**
	 * Gets the users email and logs them into the system. A list of actions they may take
	 * are displayed depending on their usertype. If the email entered is not a valid email
	 * the screen will not change and no action will be taken. 
	 */
	@Override
	public void actionPerformed(final ActionEvent theEvent) {
		if(myFrame.getData().setCurrentUser(myEmailBox.getText())) {
			List<AbstractAction> userActions = new ArrayList<AbstractAction>();
			switch(myFrame.getData().getCurrentUser().getUsertype()) { 
				case PARKMANAGER : 
					userActions.add(new CreateJobAction(myFrame));
					userActions.add(new ViewPendingJobsAction(myFrame));
					break;
				case VOLUNTEER : 
					userActions.add(new ViewMyJobsAction(myFrame));
					userActions.add(new BrowseJobsAction(myFrame));
					break;
				case STAFFMEMBER :
					userActions.add(new ViewCalendarAction(myFrame));
					userActions.add(new ViewBlackballedVolunteersAction(myFrame));
					userActions.add(new BlackballVolunteerAction(myFrame));
					break;
			}
			myFrame.createOptions(userActions);
		}
	}

}
